//
//  BasicSong.swift
//  yiplayer
//
//  Created by cgw on 2024/5/10.
//

import Foundation

class YiSong: NSObject{
    var idx = 0
    var id = "0"
    var title = ""
    var singer = ""
    var ext = "mp3"
    var album = ""
    var size: Int? = 0
    var rate = ""
    var duration: Int? = 0
    var source = ""
    var song_url = ""  //歌曲地址
    var song_file = ""
    var cover_url = ""
    var cover_file = ""
    var lyrics_url = ""
    var lyrics_text = ""
    var lyrics_file = ""
    var has_copyright = true
    
    //计算属性
    //歌曲全称：歌手-歌曲.mp3
    var name: String? {
        if singer == "" && title == "" {
            let n = Int.random(in: 100001...999999)
            return .init(format: "unknown%d.%s", n, ext)
        }
        else if singer == "" {
            return title+"."+ext
        }
        return singer+"-"+title+"."+ext
    }
    
    //不包含拓展名的全称
    var fullname: String? {
        return self.name?.replacingOccurrences(of: "."+ext, with: "")
    }
    
    @objc static func neteaseSong(_ dic: Any?) -> YiSong? {
        if let dic = dic as? [String: Any]{

            let song = YiSong()
            
            //来源
            song.source = "netease"
            
            //是否有版权
            let pri = dic["privilege"] as? [String: Any]
            let bitrate = Int.formatObject(pri?["fl"]) ?? 0
            let nocopyright = bitrate == 0
            song.has_copyright = !nocopyright
            
            //歌手名字
            if let namearr = dic["ar"] as? [[String: Any]] {
                var names:[String] = []
                for ndic in namearr {
                    if let n = String.formatObject(obj:ndic["name"]){
                        names.append(n)
                    }
                }
                if names.count == 1 {
                    song.singer = names[0]
                }else if names.count > 1 {
                    song.singer = names.joined(separator: "、")
                }
            }
            
            song.singer = song.singer.replaceInvalidCharacters()
            
            //标题
            song.title = String.formatObject(obj:dic["name"]) ?? ""
            song.title = song.title.replaceInvalidCharacters()
            
            //id
            song.id = String.formatObject(obj:dic["id"]) ?? ""
            
            //封面名称 和url
            if let aldic = dic["al"] as? [String: Any] {
                song.album = String.formatObject(obj:aldic["name"]) ?? ""
                song.cover_url = String.formatObject(obj:aldic["picUrl"]) ?? ""
            }
            
            //时长
            song.duration = (Int.formatObject(dic["dt"]) ?? 0)/1000
            
            //文件大小：并不准确，考虑逐一获取歌曲详情
            let hdic = dic["h"] as? [String :Any]
            let mdic = dic["m"] as? [String :Any]
            let ldic = dic["l"] as? [String :Any]
            if bitrate >= 320000 && hdic != nil {
                song.size = Int.formatObject(hdic?["size"]) ?? 0
            }else if bitrate >= 192000 && mdic != nil {
                song.size = Int.formatObject(mdic?["size"]) ?? 0
            }else{
                song.size = Int.formatObject(ldic?["size"]) ?? 0
            }
            return song
        }

        return nil
    }
}

class NeteaseSong: YiSong {
    
}
